home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Codigo / Jugar con las fuentes / FontMenuForm / FontMenuForm.cs next >
Encoding:
Text File  |  2002-06-18  |  2.1 KB  |  65 lines

  1. //-------------------------------------------
  2. // FontMenuForm.cs ⌐ 2001 by Charles Petzold
  3. //-------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7.  
  8. class FontMenuForm: PrintableForm
  9. {
  10.      protected string strText = "Texto de ejemplo";
  11.      protected Font font = new Font("Times New Roman", 24, FontStyle.Italic);
  12.  
  13.      public new static void Main()
  14.      {
  15.           Application.Run(new FontMenuForm());
  16.      }
  17.      public FontMenuForm()
  18.      {
  19.           Text = "Formulario men· Fuente";
  20.           Menu = new MainMenu();
  21.           Menu.MenuItems.Add("&Fuente!", new EventHandler(MenuFontOnClick));
  22.      }
  23.      void MenuFontOnClick(object obj, EventArgs ea)
  24.      {
  25.           FontDialog dlg = new FontDialog();
  26.           dlg.Font = font;
  27.  
  28.           if (dlg.ShowDialog() == DialogResult.OK)
  29.           {
  30.                font = dlg.Font;
  31.                Invalidate();
  32.           }
  33.      }
  34.      protected override void DoPage(Graphics grfx, Color clr, int cx, int cy)
  35.      {
  36.           SizeF sizef = grfx.MeasureString(strText, font);
  37.           Brush brush = new SolidBrush(clr);
  38.  
  39.           grfx.DrawString(strText, font, brush, (cx - sizef.Width) / 2,
  40.                                                 (cy - sizef.Height) / 2);
  41.      }
  42.      public float GetAscent(Graphics grfx, Font font)
  43.      {
  44.           return font.GetHeight(grfx) * 
  45.                     font.FontFamily.GetCellAscent(font.Style) /
  46.                          font.FontFamily.GetLineSpacing(font.Style);
  47.      }
  48.      public float GetDescent(Graphics grfx, Font font)
  49.      {
  50.           return font.GetHeight(grfx) * 
  51.                     font.FontFamily.GetCellDescent(font.Style) /
  52.                          font.FontFamily.GetLineSpacing(font.Style);
  53.      }
  54.      public float PointsToPageUnits(Graphics grfx, Font font)
  55.      {
  56.           float fFontSize;
  57.  
  58.           if (grfx.PageUnit == GraphicsUnit.Display)
  59.                fFontSize = 100 * font.SizeInPoints / 72;
  60.           else
  61.                fFontSize = grfx.DpiX * font.SizeInPoints / 72;
  62.  
  63.           return fFontSize;
  64.      }
  65. }